#include <Arduino.h>
#include <WiFi.h>

#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ESPDash.h>

// 无线网络名称和密码
#define SSID "LBGUEST"
#define PASSWORD "lonelybinary"

AsyncWebServer server(80);
ESPDash dashboard(&server);

Card nameCard(&dashboard, GENERIC_CARD, "设备名称");
Card temperature(&dashboard, TEMPERATURE_CARD, "温度", "°C");
Card humidity(&dashboard, HUMIDITY_CARD, "湿度", "%");

// 连接wifi
void connectWifi()
{
  /* Connect WiFi */
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PASSWORD);
  if (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.printf("WiFi Failed!\n");
    return;
  }
  delay(3000);
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void setup()
{
  Serial.begin(115200);
  connectWifi();
  // 开启web服务器
  server.begin();
  nameCard.update("DHT20 传感器");
  dashboard.sendUpdates();
}

void loop()
{
  humidity.update((int)random(0, 100));
  temperature.update((int)random(-50, 50));
  dashboard.sendUpdates();
  delay(5000);
}
